#!/bin/env/python3


from qiniu import Auth, put_file, etag, put_data
import qiniu.config

# 需要填写你的 Access Key 和 Secret Key
access_key = 'lppgndBcwOW7nfhhvsTZKBISAnbqcYdB4qibU1x9'
secret_key = 'KWGrg5hjt09eQzmYNYdvXFs2Ra_yykeURvE62Wd1'


def image_storage(image_data):
    # 构建鉴权对象
    q = Auth(access_key, secret_key)

    # 要上传的空间
    bucket_name = 'adam-zhangtiantian'

    # 上传后保存的文件名
    # 如果不指定，由七牛云来维护
    # key = 'adam.png'

    key = None
    # 生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)

    # 要上传文件的本地路径
    localfile = './logo1.png'

    # ret, info = put_file(token, key, localfile)
    ret, info = put_data(token, key, image_data)
    # 处理结果
    if info.status_code == 200:
        # print(info)
        # print(ret)
        # 返回图像名
        return ret.get("key")
    else:
        return None
    # assert ret['key'] == key
    # assert ret['hash'] == etag(localfile)


if __name__ == '__main__':
    # with测试，可以自动关闭流

    with open("./logo1.png", 'rb') as f:
        image_storage(f.read())
